package day0219

import "strconv"

func decodeString(s string) string {
	var stack1 []int
	var stack2 []byte
	n := len(s)
	i := 0
	for i < n {
		//若是数字，把数字摘出来，放到stack1
		if s[i] >= '0' && s[i] <= '9' {
			j := i
			for j < n {
				if s[j] >= '0' && s[j] <= '9' {
					j++
				} else {
					break
				}
			}
			digit, _ := strconv.Atoi(s[i:j])
			stack1 = append(stack1, digit)
			i = j
			continue
		}
		if (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || s[i] == '[' {
			stack2 = append(stack2, s[i])
			i++
			continue
		}
		if s[i] == ']' {
			t := stack1[len(stack1)-1]
			stack1 = stack1[:len(stack1)-1]
			str := ""
			k := len(stack2) - 1
			for ; k >= 0; k-- {
				if stack2[k] == '[' {
					break
				}
			}
			str = string(stack2[k:])
			stack2 = stack2[:k]
			str = str[1:]
			res := ""
			for tt := 0; tt < t; tt++ {
				res += str
			}
			stack2 = append(stack2, []byte(res)...)
			i++
			continue
		}
	}
	return string(stack2)
}
